/*
 * @Author: your name
 * @Date: 2020-08-16 14:55:09
 * @LastEditTime: 2020-08-18 21:14:05
 * @LastEditors: Please set LastEditors
 * @Description: In User Settings Edit

 * @FilePath: \demo\day02\traning\test02\main.go
 */
package main

import (
	"fmt"
	"reflect"
)

func reflectValue(x interface{}) {
	v := reflect.ValueOf(x)
	k := v.Kind()
	switch k {
	case reflect.Int:
		fmt.Println(v.Int())
	case reflect.String:
		fmt.Println(v.String())
	case reflect.Float64:
		fmt.Println(v.Float())
	}
}

type student struct {
	Name  string `json:"name"`
	Score int    `json:"score"`
}

func GetStudentInfo(t reflect.Type) {
	for i := 0; i < t.NumField(); i++ {
		field := t.Field(i)
		fmt.Printf("name:%s index:%d type:%v json tag:%v\n", field.Name, field.Index, field.Type, field.Tag.Get("json"))
	}

	if scoreField, ok := t.FieldByName("Score"); ok {

		fmt.Printf("name:%s index:%d type:%v json tag:%v\n", scoreField.Name, scoreField.Index, scoreField.Type, scoreField.Tag.Get("json"))

	}
}

func main() {

	// -------------------------------
	// var a int = 100
	// var b string = "哈哈"
	// var c float64 = 3.14

	// reflectValue(a)
	// reflectValue(b)
	// reflectValue(c)
	// -------------------------------

	stu1 := student{
		Name:  "小王子",
		Score: 90,
	}
	t := reflect.TypeOf(stu1)
	fmt.Println(t.Name(), t.Kind())
	GetStudentInfo(t)
}
